% Chapter 2. Section 6.

\section{Debugger}
\label{debugger}
Almost no software is there without error in it. Usually developers spend more time finding out the problems and fixing them than developing the software. There are several ways to do it. One may go through the source code and try to find the error by hand, one would add trace output and analyse the trace. But still the most convenient way is to {\bf debug}.

\subsection{What is a debugger}
Debugger is a program which allows developers to find errors in other programs. There are debuggers for programming languages, scripting languages, database query languages like SQL. Using the debugger it is possible to watch the program execution step by step. The program execution can be suspended at some certain point or depending on some expression value. All the variables and register values can be examined through the debuggers. All these features make debuggers an irreplaceable tool for the software development. There are many debuggers available for free software developers. The most popular and most powerful of them would be the {\bf GNU Debugger: GDB}. Documentation about the GDB and the project itself can be found on the project's official site: \textit { www.gnu.org/software/gdb}.

\subsection{Embedding debug information}
Usually programs cannot be decoded to the source code on any high level language like "C". The highest available language via the reverse engineering is the assembly language. Unfortunately the assembly language is not convenient for debugging the program. That's why the compilers usually allow developers to embed some debug information into the program. That information also contains the source code of the program. Debuggers can use that information to allow the developers to debug the program. 

The GCC compiler also may allow developers to embed debugging information into the compiled program files. To enable debugging one should use the following command:

\textit { g++ -g my\_prog.cpp -o my\_prog}

\subsection{Using GDB}
Let's see how GDB can be used to debug a certain program. GDB provides only console interface. Most developers would not be familiar with such interface in case of debuggers but still it is one of the best debuggers available freely that's why we need to get familiar with it. First of all the program which is to be debugged needs to be loaded into the GDB with the following command:

\textit { gdb my\_program }

After the program is successfully loaded, usually some breakpoints need to be set. The breakpoint can be set using the following command:

\textit { (gdb) b 15 }

The first part is the GDB prompt. This command will set a breakpoint on line 15. Then the loaded program must be executed:

\textit { (gdb) run }

Shortly the execution will be suspended and GDB will wait for further commands. Usually one needs to \textit { step} into some function body or just go to the \textit { next} line of code. The corresponding GDB commands are outlined.

It sometimes makes sense to see "where we are". Thus, to find out which functions are called at the moment. That information (the backtrace) can be accessed via the GDB \textit { bt} command.

The mentioned commands are suitable for really simple case (when the programs runs normally, but has some wrong output). Usually things are much worse. The program can crash (segmentation fault or other faults may occur) which makes hard to set some certain breakpoints and debug it. Even worse the program can be compiled without debug information embedded or the source code may not be available. Still GDB can help in all those cases. One can analyse the core file dumped on program crash (just executing GDB and passing the name of the program and the core file one after another). GDB provides mapping between memory addresses and lines of code which allows to find the line of where the fault was produced. 

It is impossible to list all the GDB features here. For a complete documentation see the book "Debugging with GDB" (www.gnu.org/s/gdb/documentation).

%\subsection{Graphical wrappers for GDB}
